<?php
header('Content-Type: text/html; charset=utf-8');
session_start();
session_regenerate_id();

//-----------------------------------------------------------------------------
require_once('apps/bootstrap.php');

register_shutdown_function(function() {
    // session save 
	dtoTwitterAuth::factory(dtoTwitterAuth::factory());
	dtoFacebookAuth::factory(dtoFacebookAuth::factory());
	session_write_close();
});

//-----------------------------------------------------------------------------
// Twitter

// Login
$oTwitter   = new cAppTwitter(dtoTwitterAuth::factory());
$ac         = $oTwitter->getAccessToken(isset($_GET['oauth_token']) ?
                                        $_GET['oauth_token'] : NULL);
// Not signed in
if ($ac === NULL)
{
    $rurl = $oTwitter->getRedirectUrl();
    header("Location: {$rurl}");
    exit(0);
}

// Get some infos
$twCfg = $oTwitter->getCfg();
$oTwitterFetch = new OAuth( $twCfg['key']['consumer_key'],
                            $twCfg['key']['consumer_secret'],
                            OAUTH_SIG_METHOD_HMACSHA1,
                            OAUTH_AUTH_TYPE_URI);
$oTwitterFetch->setToken(   $oTwitter->getSession()->oauth_token,
                            $oTwitter->getSession()->oauth_token_secret);
try
{
    $oTwitterFetch->fetch($twCfg['fetch']['friends_timeline']);
    var_dump(json_decode($oTwitterFetch->getLastResponse()));
}
catch (Exception $e)
{
    dtoTwitterAuth::factory()->access_token = FALSE;
}

//-----------------------------------------------------------------------------
// Facebook

// Login
/*$oFacebook 		= new cAppFacebook(dtoFacebookAuth::factory());
$ac			= $oFacebook->getAccessToken(isset($_GET['code']) ?
                                                    $_GET['code'] : NULL);
// Not signed in
if ($ac === NULL)
{
    $rurl = $oFacebook->getRedirectUrl();
    header("Location: {$rurl}");
    exit(0);
}

// Get some infos
$fbCfg = $oFacebook->getCfg();
$oFacebookFetch = new OAuth($fbCfg['key']['consumer_key'],
                            $fbCfg['key']['consumer_secret'],
                            OAUTH_SIG_METHOD_HMACSHA1,
                            OAUTH_AUTH_TYPE_URI);
$oFacebookFetch->setToken(  $oFacebook->getSession()->oauth_token,
                            $fbCfg['key']['consumer_secret']);
try
{
    $params = array('access_token'  => $oFacebook->getSession()->oauth_token,
                    'limit'         => 1);
    $uri    = uri_builder($fbCfg['fetch']['news_feed'], $params);
    $oFacebookFetch->fetch($uri);
    var_dump(json_decode($oFacebookFetch->getLastResponse()));
}
catch (Exception $e)
{
    dtoFacebookAuth::factory()->access_token = FALSE;
}*/

# EOF
